package main

import "fmt"

/*
* Author: JacksonCoder
* CreateTime: 2024/11/30
 */

func ForLoop() {
	for i := 0; i < 10; i++ {
		println(i)
	}

	for i := 0; i < 10; {
		println(i)
		i++
	}

}

func Loop2() {
	i := 0
	for i < 10 {
		i++
		println(i)
	}

	//for true {
	//	i++
	//	println(i)
	//}

	// while

	//do
}

func ForArr() {
	arr := [3]int{1, 2, 3}
	for index, value := range arr {
		println("下标index: ", index, " 值: ", value)
	}
	for index := range arr {
		println("下标index: ", index, " 值: ", arr[index])
	}
}

func ForSlice() {
	slice := []string{"alex", "wusir", "jack", "alen", "alice"}
	for index, value := range slice {
		println("下标index: ", index, " 值: ", value)
	}
}

func ForMap() {
	m := map[string]int{
		"key1": 100,
		"key2": 120,
		"key3": 130,
	}
	for k, v := range m {
		println(k, v)
	}
	for k := range m {
		println(k, m[k])
	}
}

// 不要对迭代对象去取地址
func LoopBug() {
	users := []User{
		{name: "小红"},
		{name: "小明"},
		{name: "小张"},
	}
	m := make(map[string]*User)
	for _, u := range users {
		fmt.Printf("%p \n", &u)
		m[u.name] = &u
	}

	fmt.Printf("%v \n", m)

}

type User struct {
	name string
}

func LoopBreak() {
	i := 0
	for true {
		if i > 10 {
			break
		}
		i++
	}
}

func LoopContinue() {
	i := 0
	for i < 10 {
		i++
		if i%2 == 1 {
			continue
		}
		println(i)
	}
}
